#include <Wire.h>
#include <ArduinoJson.h>

const int dianjiPin =  13;
int dianjiState = LOW;
const int buttonPin = A1; 
const int esp32Pin = A4; 

int inputValue;
int inputValueMax=0;

//int  MinSoilMoisture;
#define MinSoilMoisture 20

void setup() {
  // put your setup code here, to run once:
   pinMode(dianjiPin,OUTPUT);
   Serial.begin(9600);
   pinMode(buttonPin,INPUT);
   pinMode(esp32Pin,OUTPUT);
   
   inputValueMax = analogRead(buttonPin);
}

void loop() {
  // put your main code here, to run repeatedly:
  inputValue = analogRead(buttonPin);     //这里读入土壤湿度传感器的值，在水中，即湿度最大时为0，空气中为约300-500的周期值
  
  if(inputValue>inputValueMax) inputValueMax=inputValue;
  
  int  SoilMoisture=(1-inputValue*1.0/inputValueMax)*100;  //换算成土壤湿度百分比
  analogWrite(esp32Pin, SoilMoisture);
  Serial.println(SoilMoisture);
  Serial.println(esp32Pin);
  Serial.println();
  
  //Serial.println(inputValue);
  if(SoilMoisture<MinSoilMoisture)
  {
    dianjiState = HIGH;
    digitalWrite(dianjiPin, dianjiState);
    delay(1500);
    dianjiState = LOW;
    digitalWrite(dianjiPin, dianjiState);
  }
  delay(5000);
}
